package com.justgame.project.cloud.common.query;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.justgame.project.cloud.common.constant.SerializableFunction;
import com.justgame.project.cloud.base.entity.BaseEntity;

import java.util.Arrays;

public class Column {
    private final String[] columns;

    /**
     * 全部字段
     */
    public static Column all(){
        return new Column(new String[]{StringPool.ASTERISK});
    }

    protected Column(String[] columns) {
        this.columns = columns;
    }

    public String[] getColumns() {
        return columns;
    }

    public static Builder builder(){
        return new Builder();
    }

    public static class Builder{
        private String[] columns = new String[]{StringPool.ASTERISK};

        protected Builder() {
        }

        protected Builder(String[] columns) {
            this.columns = columns;
        }

        public Builder string(String ...columnStr){
            this.columns = columnStr;
            return this;
        }

        @SafeVarargs
        public final <E extends BaseEntity> Builder fields(SerializableFunction<E, ?>... functions){
            columns = Arrays.stream(functions).map(SerializableFunction::getFieldName).toArray(String[]::new);
            return this;
        }

        public Column build(){
            return new Column(this.columns);
        }
    }
}
